//
// Created by daiyizheng on 2022/4/12.
//
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
    //dp[i][0][0] = 0
    //dp[i][k][0] = max(dp[i-1][k][0], dp[i-1][k][1]+price[i])
    //dp[i][k][1] = max(dp[i-1][k][1], dp[i-1][k-1][0]-price[i])
    int maxProfit(vector<int>& prices) {
        int n = prices.size();

        //定义数组
        vector<vector<int>> dp(n, vector<int>(2));
        //初始化
        dp[0][0] = 0;
        dp[0][1] = -prices[0];

        //状态转移
        for (int i = 1; i < n; ++i) {
            dp[i][0] = max(dp[i-1][0], dp[i-1][1]+prices[i]);
            dp[i][1] = max(dp[i-1][1], -prices[i]);
        }
        return dp[n-1][0];
    }


    //空间压缩
    int maxProfit1(vector<int>& prices) {
        int profit0 = 0;
        int profit1 = -prices[0];

        for (int i = 1; i < prices.size(); ++i) {
            profit0 = max(profit0, profit1+prices[i]);
            profit1 = max(profit1, -prices[i]);
        }
        return profit0;
    }
};